/*
 * @Author: 来自火星的码农 15122322+heyzhi@user.noreply.gitee.com
 * @Date: 2025-10-26 21:19:03
 * @LastEditors: 来自火星的码农 15122322+heyzhi@user.noreply.gitee.com
 * @LastEditTime: 2025-10-30 15:50:29
 * @FilePath: /GateServer/src/statusclient/statusclient.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "statusclient.h"
#include <algorithm>
#include <grpcpp/impl/codegen/client_context.h>
#include <grpcpp/impl/codegen/status.h>
#include "../util/util.h"
#include "../util/config.h"
#include "statusconpool.h"
namespace Yizhi {

    static  ConfigVar<std::string>::ptr g_status_server_host=ConfigMgr::Lookup("statusserver.host",(std::string)"127.0.0.1","");
    static ConfigVar<int>::ptr g_status_server_port=ConfigMgr::Lookup("statusserver.port",(int)50052,"");
    
    StatusClient::StatusClient(){
        m_conpool.reset(new StatusConPool(g_status_server_host->getvalue(),g_status_server_port->getvalue(),5));
    }

    
    message::GetchatServiceRsp StatusClient::GetChatService(int uid){
        message::GetchatServiceRsp rsp;
        message::GetchatServiceReq reqest;
        reqest.set_uid(uid);
        grpc::ClientContext context;
        auto con=m_conpool->Getconnection();


        if(con==nullptr){
            rsp.set_error(Ecode::RPCFLASE);
            LOG_ERROR("status server rpc service is nullptr");
            return rsp;
        }
        grpc::Status staus=con->GetChatService(&context, reqest,&rsp);

        if(!staus.ok()){
            rsp.set_error(Ecode::RPCFLASE);
        }
        Defer defer([this, &con]() mutable {
            m_conpool->ReturnCon(std::move(con));
        });
        
        return rsp;
    }
}